package com.ncloud.common.exception;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ncloud.common.body.Result;
import com.ncloud.common.utils.ResultUtil;

@ControllerAdvice
public class GlobalExceptionHandler {
	@ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result<?> handle(Exception e){
        if(e instanceof UserException){
            UserException base = (UserException) e;
            e.printStackTrace();
            return ResultUtil.error(base.getErrorCode(),base.getMessage());
        }else if (e instanceof NullPointerException){
            e.printStackTrace();
            return ResultUtil.error(-2,"空指针异常！"+e.toString());
        }else{
            e.printStackTrace();
            return ResultUtil.error(-1,e.getMessage());
        }
    }
}